package com.ruoyi.framework.interceptor;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.ruoyi.common.core.domain.model.LoginStudent;
import lombok.experimental.UtilityClass;

/**
 * 全链路追踪
 * thirdSession工具类
 *
 * @author byChen
 * @date 2021/11/22
 */
@UtilityClass
public class ApiSessionHolder {
    private final ThreadLocal<LoginStudent> THREAD_LOCAL_THIRD_SESSION = new TransmittableThreadLocal<>();

    /**
     * TTL 设置 当前登录学员信息
     *
     * @param loginStudent 登录学员信息
     */
    public void setLoginStudent(LoginStudent loginStudent) {
        THREAD_LOCAL_THIRD_SESSION.set(loginStudent);
    }

    /**
     * 获取TTL中的LoginStudent
     *
     * @return 登录学员信息
     */
    public LoginStudent getLoginStudent() {
        return THREAD_LOCAL_THIRD_SESSION.get();
    }


    /**
     * 获取学员id
     *
     * @return 学员id
     */
    public Long getStudentId() {
        if (getLoginStudent() != null) {
            return getLoginStudent().getId();
        }
        return null;
    }


    /**
     * 清除
     */
    public void clear() {
        THREAD_LOCAL_THIRD_SESSION.remove();
    }
}
